package com.jbinfo.qingcaowan.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.PropertyConfigurator;
import org.junit.Test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

/**
 * 
* @ClassName: Generator 
* @Description: TODO Mybatis按照xml配置自动生成工具
* @author ZhangLei
* @date 2016年5月24日 下午1:42:36
 */
public class Generator {

	public static final String CONFIG_STRING = "G:/Work/QcwRead/Src/qingcaowan_web/src/main/resources/log4j.properties";
	public static final String CONTEXT_STRING = "G:/Work/QcwRead/Src/qingcaowan_web/src/test/resources/jdbc/generatorConfig.xml";

	/**
	 * @param args
	 */
	@Test
	public void generateCode() {
		PropertyConfigurator.configure(CONFIG_STRING);
		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;

		// 获取配置文件generatorConfig
		File configFile = new File(CONTEXT_STRING);
		ConfigurationParser cp = new ConfigurationParser(warnings);
		try {
			Configuration config = cp.parseConfiguration(configFile);
			DefaultShellCallback callback = new DefaultShellCallback(overwrite);
			MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
			myBatisGenerator.generate(null);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
